package core.proto;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;

import app.exceptions.CollabVizException;

//LEGACY CODE!!!

public class ToolbarLegacy extends JPanel
{

    private static final long serialVersionUID = 1L;
    
    public JButton back;
    public JButton forward;
    public JButton close;
    public JTextField search = new JTextField(15);
    public ArrayList<String> history= new ArrayList<String>();
    public int pos=-1;
    public int max=-1;
    public int window;

    
    public ToolbarLegacy(int window){
     this.window=window;
     back= new JButton(new ImageIcon( Util.getResourceUrl("/img/back.png" ) ));
     forward= new JButton(new ImageIcon( Util.getResourceUrl("/img/forward.png" ) ));
     close= new JButton(new ImageIcon( Util.getResourceUrl("/img/close.png" ) ));
     setLayout(new FlowLayout());
     ButtonHandler bh= new ButtonHandler();
     back.addActionListener( bh);
     back.setEnabled( false );
     back.setVisible( true );
     forward.addActionListener(bh);
     forward.setEnabled( false );
     forward.setVisible( true );
     close.addActionListener(bh);
     close.setEnabled( true);
     close.setVisible(true);
     search.addActionListener( new SearchHandler() );
     add(back);
     add(forward);
     add(search);
     add(close);
     setVisible(true);
    }
    
    public void update(String path){
        if(max==pos){
            max++;    
        }else{
            for(int i=pos+1;i<=max;i++){
                history.remove( i );
            }
            max=pos+1;
        }
        history.add( path );
        pos++;
        if(pos>0){
            back.setEnabled( true );
        }
    }
    
    private class ButtonHandler implements ActionListener{

        public void actionPerformed( ActionEvent arg0 ) {
            if(arg0.getSource().equals( back )){
                pos--;
                forward.setEnabled( true );
            }else if(arg0.getSource().equals( forward )){
                pos++;
                if(pos<max){
                forward.setEnabled( true );}

            }else if(arg0.getSource().equals( close )){
                search.setText( "" );
            }
//            try {
//                AllComponents.updateDoc( history.get( pos),false,window );
//            } catch( IOException e ) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            } catch( CollabVizException e ) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
        }
        
    }
    
    private class SearchHandler implements ActionListener{

        public void actionPerformed( ActionEvent arg0 ) {
            String term= search.getText();
            //AllComponents.searchDoc( term, window );
        }
        
    }

    public void repaintButton() {
        // TODO Auto-generated method stub
        if(max==pos){
            forward.setEnabled( false );
        }else{
            forward.setEnabled( true );
        }
        if(pos<1){
            back.setEnabled( false );
        }else{
            back.setEnabled( true );
        }
        repaint();
    }
}
